package com.todod.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.todod.annotaion.NotLogging;
import com.todod.entity.QueryEntry;
import com.todod.entity.ResultData;
import com.todod.model.Dict;
import com.todod.service.IDictService;

/**
 * 数据字典
 * 
 * @author MengHaoPeng
 **/
@RestController
@RequestMapping("/dict")
public class DictController extends BaseManageController {

	@Autowired
	private IDictService dictService;

	/**
	 * @Title: getDictListByCode
	 * @Description:根据父字典code获取获取数据字典集合
	 * @author MengHaoPeng
	 * @date 2019-10-09
	 * @return ResultData
	 */
	@NotLogging
	@RequestMapping("/getDictListByCode")
	public ResultData getDictListByCode(String code) {
		List<Dict> list = dictService.getDictListByCode(code);
		return ResultData.success("ok", list);
	}

	/**
	 * @Title: getSysDictListByCode
	 * @Description:根据字典code获取获取数据字典集合
	 * @author MengHaoPeng
	 * @date 2019-10-09
	 * @return ResultData
	 */
	@RequestMapping("/getSysDictListByCode")
	public ResultData getSysDictListByCode(String code) {
		List<Dict> list = dictService.getSysDictListByCode(code);
		return ResultData.success("ok", list);
	}

	/**
	 * getList
	 * 
	 * @title 获取业务字典列表
	 * @author MengHaoPeng
	 * @date 2019-10-11
	 * @return List<Dict>
	 */
	@RequestMapping("/getList")
	public ResultData getList(@RequestBody QueryEntry queryEntry) {
		List<Dict> list = dictService.getList(queryEntry);
		return ResultData.success("ok", list);
	}

	/**
	 * getParentList
	 * 
	 * @title 获取父字典列表
	 * @author MengHaoPeng
	 * @date 2019-10-11
	 * @return List<Dict>
	 */
	@RequestMapping("/getParentList")
	public ResultData getParentList(String code) {
		List<Dict> list = dictService.getParentList(code);
		return ResultData.success("ok", list);
	}

	/**
	 * getListByParentCode
	 * 
	 * @title 获取父字典列表
	 * @author MengHaoPeng
	 * @date 2019-10-11
	 * @return List<Dict>
	 */
	@RequestMapping("/getListByParentCode")
	public ResultData getListByParentCode(String parentCode) {
		List<Dict> list = dictService.getListByParentCode(parentCode);
		return ResultData.success("ok", list);
	}

	/**
	 * @title 新增数据
	 * @author MengHaoPeng
	 * @date 2019-10-11
	 * @return List<Dict>
	 */
	@RequestMapping("/saveData")
	public ResultData saveData(@RequestAttribute Long _userId, @RequestBody Dict dict, Long pId) {

		dictService.saveData(_userId, dict, pId);

		return ResultData.success("ok");
	}

	/**
	 * @title 编辑数据
	 * @author MengHaoPeng
	 * @date 2019-10-11
	 * @return List<Dict>
	 */
	@RequestMapping("/editData")
	public ResultData editData(@RequestAttribute Long _userId, @RequestBody Dict dict) {

		dictService.editData(_userId, dict);
		return ResultData.success("ok");
	}

	/**
	 * 启用
	 * 
	 * @Title: enable
	 * @Description:
	 * @author gsh
	 * @date 2020年4月12日
	 * @param _userId
	 * @param id
	 * @return
	 */
	@PostMapping("/enable")
	public ResultData enable(@RequestAttribute Long _userId, Long id) {

		dictService.updateState(_userId, id, "1");
		return ResultData.success();
	}

	/**
	 * 禁用
	 * 
	 * @Title: enable
	 * @Description:
	 * @author gsh
	 * @date 2020年4月12日
	 * @param _userId
	 * @param id
	 * @return
	 */
	@PostMapping("/disable")
	public ResultData disable(@RequestAttribute Long _userId, Long id) {

		dictService.updateState(_userId, id, "2");
		return ResultData.success();
	}

	/**
	 * @Title: getDictListByParentCodeAndCode
	 * @Description:根据父字典code获取获取数据字典集合
	 * @author MengHaoPeng
	 * @date 2019-10-09
	 * @return ResultData
	 */
	@NotLogging
	@RequestMapping("/getDictEntryBesidesCode")
	public ResultData getDictEntryBesidesCode(String parentCode, String code) {

		List<String> typeList = new ArrayList<>();

		String[] idArray = code.split(",");

		for (int i = 0; i < idArray.length; i++) {
			typeList.add(idArray[i]);
		}

		List<Dict> list = dictService.getDictByParentCodeBesidesCode(parentCode, typeList);
		return ResultData.success("ok", list);
	}

}
